﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Plumber.Accounting.ModuleData;

namespace Plumber.Accounting.Library
{
    class LiquidationTaxUpdater
    {
        private static  DRLiquidationTax drLiquidationTax = null;
        private static double previousCredit = 0;

        public static void UpdateImport(int month, int year, TaxRegisterType taxregister, TaxRegisterStatus status, double amount)
        {
            if (drLiquidationTax == null)
                drLiquidationTax = new DRLiquidationTax(true);

            bool found = drLiquidationTax.Find(month, year);
            if (!found)
            { 
                previousCredit = ReadPrevoiusCredit(month, year);

                drLiquidationTax.AddRecord       ();
                drLiquidationTax.SetValue<int>   (PL_LiquidationTax.Month, month);
                drLiquidationTax.SetValue<int>   (PL_LiquidationTax.Year, year);
                drLiquidationTax.SetValue<double>(PL_LiquidationTax.PrevoiusCredit, previousCredit);
                drLiquidationTax.SetValue<double>(PL_LiquidationTax.CurrentCredit, 0);
                drLiquidationTax.SetValue<double>(PL_LiquidationTax.SaleCredit, 0);
                drLiquidationTax.SetValue<double>(PL_LiquidationTax.SaleDebit, 0);
                drLiquidationTax.SetValue<double>(PL_LiquidationTax.PurchaseCredit, 0);
                drLiquidationTax.SetValue<double>(PL_LiquidationTax.PurchaseDebit, 0);
                drLiquidationTax.SetValue<TaxRegisterStatus>(PL_LiquidationTax.SaleRegister, TaxRegisterStatus.E_NotPrinted);
                drLiquidationTax.SetValue<TaxRegisterStatus>(PL_LiquidationTax.PurchaseRegister, TaxRegisterStatus.E_NotPrinted);
                drLiquidationTax.SetValue<string>(PL_LiquidationTax.JournalEntryNo, "");
            }
            switch(taxregister)
            {
                case TaxRegisterType.E_TaxPurchase:
                    drLiquidationTax.SetValue<double>(PL_LiquidationTax.PurchaseCredit, amount);
                    drLiquidationTax.SetValue<TaxRegisterStatus>(PL_LiquidationTax.PurchaseRegister, status);
                    break;

                case TaxRegisterType.E_TaxSale:
                    drLiquidationTax.SetValue<double>(PL_LiquidationTax.SaleDebit, amount);
                    drLiquidationTax.SetValue<TaxRegisterStatus>(PL_LiquidationTax.SaleRegister, status);
                    break;
            }

            // Se ho stampato entrambi i registri, calcolo l'eventuale credito
            //if (drLiquidationTax.GetValue<TaxRegisterStatus>(PL_LiquidationTax.PurchaseRegister) != TaxRegisterStatus.E_NotPrinted &&
            //    drLiquidationTax.GetValue<TaxRegisterStatus>(PL_LiquidationTax.SaleRegister) != TaxRegisterStatus.E_NotPrinted)
            //{
                double toPay =  drLiquidationTax.GetValue<double>(PL_LiquidationTax.SaleDebit) -
                                drLiquidationTax.GetValue<double>(PL_LiquidationTax.PrevoiusCredit) - 
                                drLiquidationTax.GetValue<double>(PL_LiquidationTax.PurchaseCredit);
                drLiquidationTax.SetValue<double>(PL_LiquidationTax.CurrentCredit, toPay < 0 ? -toPay : 0);
            //}

            drLiquidationTax.Update();
        }

        private static double ReadPrevoiusCredit(int month, int year)
        {
            DRLiquidationTax drLiquidationtax = new DRLiquidationTax(false);
            month--;
            if (month == 0)
            {
                month = 12;
                year--;
            }

            if (drLiquidationtax.Find(month, year))
                return drLiquidationtax.GetValue<double>(PL_LiquidationTax.CurrentCredit);
            else 
                return 0;
        }
    }
}
